package com.atguigu.syt.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.service.exception.GuiguException;
import com.atguigu.common.util.result.ResultCodeEnum;
import com.atguigu.syt.cmn.client.DictFeignClient;
import com.atguigu.syt.cmn.client.RegionFeignClient;
import com.atguigu.syt.enums.DictTypeEnum;
import com.atguigu.syt.hosp.repository.HospitalRepository;
import com.atguigu.syt.hosp.service.HospitalService;
import com.atguigu.syt.model.hosp.Hospital;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;
    @Resource
    private DictFeignClient dictFeignClient;

    @Resource
    private RegionFeignClient regionFeignClient;

    /**
     * 根据医院编码获取医院详情
     * @param paramMap
     * @return
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        Hospital hospital =
                JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        Hospital byHoscode = hospitalRepository.findByHoscode(hospital.getHoscode());
        if (hospital != null){
            hospital.setStatus(byHoscode.getStatus());
            hospital.setId(byHoscode.getId());
            hospitalRepository.save(hospital);
        } else {
            hospital.setStatus(1);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        hospitalRepository.findByHoscode(hoscode);
        return null;
    }

    /**
     * 封装Hospital数据
     * @param hospital
     * @return
     */
    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getName(DictTypeEnum.HOSTYPE.getDictTypeId(), hospital.getHostype());
        String provinceString = regionFeignClient.getName(hospital.getProvinceCode());
        String cityString = regionFeignClient.getName(hospital.getCityCode());
        if(provinceString.equals(cityString)) cityString = "";
        String districtString = regionFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
        return hospital;
    }

    @Override
    public Hospital show(String hoscode) {

        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return this.packHospital(hospital);
    }

    /**
     * 根据医院名称分页查询医院列表
     * @param page
     * @param limit
     * @param hosname
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, String hosname) {
        PageRequest pageRequest = PageRequest.of(page-1, limit, Sort.by(Sort.Direction.ASC, "hoscode"));
        Page<Hospital> pages = null;
        if (StringUtils.isEmpty(hosname)){
            pages =  hospitalRepository.findAll(pageRequest);
        } else {
            pages =  hospitalRepository.findByHosnameLike(hosname, pageRequest);
        }
        pages.getContent().forEach(this::packHospital);
        return pages;
    }

    /**
     * 根据医院编码修改医院状态
     * @param hoscode
     * @param status
     */
    @Override
    public void updateStatus(String hoscode, Integer status) {
        if (status.intValue() == 0 || status.intValue() == 1){
            Hospital hospital = hospitalRepository.findByHoscode(hoscode);
            hospital.setStatus(status);
            hospitalRepository.save(hospital);
            return;
        }
        throw new GuiguException(ResultCodeEnum.PARAM_ERROR);
    }
}
